package com.guigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.guigu.constant.MessageConstant;
import com.guigu.entity.PageResult;
import com.guigu.entity.QueryPageBean;
import com.guigu.entity.Result;
import com.guigu.pojo.CheckItem;
import com.guigu.pojo.EvaluationSuggestions;
import com.guigu.service.EvaluationSuggestionsService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/Eval")
public class EvaluationSuggestionsController {
    @Reference
    private EvaluationSuggestionsService evalService;

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult result = evalService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );


         return result;
    }

    @RequestMapping("/findById")
    public Result findById(Integer id){
        try {
            EvaluationSuggestions eval=evalService.findById(id);
            return new Result(true,MessageConstant.QUERY_EVALUATION_SUCCESS,eval);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_EVALUATION_FAIL);
        }
    }


    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {

            boolean delete = evalService.delById(id);

        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_EVALUATION_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_EVALUATION_SUCCESS);
    }

    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody EvaluationSuggestions eval) throws IOException {
        try {
            eval.setCreatetime(new Date());
            evalService.add(eval);
        }catch (Exception e){
            return new Result(false, MessageConstant.ADD_EVALUATION_FAIL);
        }
        return new Result(false, MessageConstant.ADD_EVALUATION_SUCCESS);
    }

    //修改
    @RequestMapping("/edit")
    public Result edit(@RequestBody EvaluationSuggestions eval) throws IOException {
        try {
            eval.setCreatetime(new Date());
            evalService.edit(eval);
        }catch (Exception e){
            return new Result(false, MessageConstant.EDIT_EVALUATION_FAIL);
        }
        return new Result(false, MessageConstant.EDIT_EVALUATION_SUCCESS);
    }


}
